/*
 *     Prionace - A file navigator to manage your files
 *
 *     Copyright (C) 2012 Francivan Bezerra
 *
 *     This file is part of Prionace.
 *
 *     Prionace is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     Prionace is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with Prionace.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.fba.prionace.ui;

import java.awt.Component;
import java.awt.Cursor;
import java.awt.event.ContainerEvent;
import java.awt.event.ContainerListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import org.fba.prionace.file.JFile;

public class PanesManager implements ContainerListener, MouseListener,
		PathChangeListener {

	private ShortcutsPane shortcutsPane;

	private FilesPane filesPane;

	public PanesManager(ShortcutsPane shortcutsPane, FilesPane filesPane) {
		setShortcutsPane(shortcutsPane);
		setFilesPane(filesPane);
		init();
	}

	private void init() {
		Component[] components = shortcutsPane.getButtonsPane().getComponents();

		for (Component component : components) {
			if (component instanceof JFile) {
				((JFile) component).addMouseListener(this);
			}
		}
	}

	public ShortcutsPane getShortcutsPane() {
		return shortcutsPane;
	}

	public void setShortcutsPane(ShortcutsPane shortcutsPane) {
		shortcutsPane.getButtonsPane().addContainerListener(this);
		this.shortcutsPane = shortcutsPane;
	}

	public FilesPane getFilesPane() {
		return filesPane;
	}

	public void setFilesPane(FilesPane filesPane) {
		filesPane.getPathBar().addPathChangeListener(this);
		this.filesPane = filesPane;
	}

	public void componentAdded(ContainerEvent e) {
		JFile added = shortcutsPane.getShortcuts().lastElement();
		added.addMouseListener(this);
	}

	public void componentRemoved(ContainerEvent e) {
	}

	public void mouseClicked(MouseEvent e) {
		JFile clicked = (JFile) e.getSource();
		boolean roots = clicked.getActionCommand().equals("/")
				&& System.getProperty("os.name").toLowerCase().contains("win");

		shortcutsPane.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));

		if (e.getButton() == MouseEvent.BUTTON1) {
			if (roots) {
				filesPane.showRoots();
			} else {
				filesPane.setCurrentPath(clicked.getFile());
				filesPane.refreshBars();
			}
		} else if (e.getButton() == MouseEvent.BUTTON2) {
			filesPane.addViewer(clicked.getFile());
			if (roots) {
				filesPane.showRoots();
			}
		}

		shortcutsPane.setCursor(null);
	}

	public void mousePressed(MouseEvent e) {
	}

	public void mouseReleased(MouseEvent e) {
	}

	public void mouseEntered(MouseEvent e) {
	}

	public void mouseExited(MouseEvent e) {
	}

	public void pathChanged(PathChangeEvent e) {
		PathBar path = (PathBar) e.getSource();
		shortcutsPane.selectShortcutFor(path.getCurrentPath());
	}
}
